package arkkomeet

import Arkkomeet.CatalogoParticipante

class CatalogoParticipanteService {

    def busquedaFiltrada(params) {
		def oCatalogoParticipante = new CatalogoParticipante(params)
		String nombre = params.catPartNombre
		String apellido1 = params.catPartApellido1
		String apellido2 = params.catPartApellido2
		String correoElectronico = params.catPartCorreoElectronico 
		String procedencia = params.catPartProcedencia
		if(nombre.equals("")){
			nombre = null
		}
		if(apellido1.equals("")){
			apellido1 = null
		}
		if(apellido2.equals("")){
			apellido2 = null
		}
		if(correoElectronico.equals("")){
			correoElectronico = null
		}
		if(procedencia.equals("")){
			procedencia = null
		}
		
		def listaParticipantes = CatalogoParticipante.findAll("FROM CatalogoParticipante cp WHERE"+
			"(? IS NULL OR cp.catPartNombre like  concat(?,'%')) AND (? IS NULL OR cp.catPartApellido1 like  concat(?,'%'))"+
			"AND (? IS NULL OR cp.catPartApellido2 like  concat(?,'%'))AND (? IS NULL OR cp.catPartCorreoElectronico like  concat(?,'%'))"+
			"AND (? IS NULL OR cp.catPartProcedencia like  concat(?,'%'))", 
			[nombre, nombre, apellido1, apellido1, apellido2, apellido2, correoElectronico, correoElectronico, procedencia, procedencia])
		return listaParticipantes
		
    }
	
	def formatoNombre(params){
		def oCatalogoParticipante = new CatalogoParticipante(params)
		String nombre = params.catPartNombre
		char[] caracteres =nombre.toCharArray();
		caracteres[0] = Character.toUpperCase(caracteres[0]);
		for (int i = 1; i < nombre.length(); i++){
		if (caracteres[i] == ' ' || caracteres[i] == '.' || caracteres[i] == ','){
		  caracteres[i + 1] = Character.toUpperCase(caracteres[i + 1])
		  i++
		}
		else{		
		caracteres[i] = Character.toLowerCase(caracteres[i])
		}
		}
		
		  return new String(caracteres)		  
	}
	def formatoApellido1(params){
		def oCatalogoParticipante = new CatalogoParticipante(params)
		String apellido1 = params.catPartApellido1
		char[] caracteres =apellido1.toCharArray();
		caracteres[0] = Character.toUpperCase(caracteres[0]);
		for (int i = 1; i < apellido1.length()-1; i++)
		if (caracteres[i] == ' ' || caracteres[i] == '.' || caracteres[i] == ','){
		  caracteres[i + 1] = Character.toLowerCase(caracteres[i + 1]);
		 }
		else{
		caracteres[i] = Character.toLowerCase(caracteres[i]);
			}
		  return new String(caracteres);
	}
	def formatoApellido2(params){
		def oCatalogoParticipante = new CatalogoParticipante(params)
		String apellido2 = params.catPartApellido2
		char[] caracteres =apellido2.toCharArray();
		caracteres[0] = Character.toUpperCase(caracteres[0]);
		for (int i = 1; i < apellido2.length()-1; i++)
		if (caracteres[i] == ' ' || caracteres[i] == '.' || caracteres[i] == ','){
		  caracteres[i + 1] = Character.toLowerCase(caracteres[i + 1]);
		 }
		else{
		caracteres[i] = Character.toLowerCase(caracteres[i]);
			}
		  return new String(caracteres);
	}
	def formatoObservaciones(params){
		def oCatalogoParticipante = new CatalogoParticipante(params)
		String observaciones = params.catPartObservaciones
		char[] caracteres =observaciones.toCharArray();
		caracteres[0] = Character.toUpperCase(caracteres[0]);
		for (int i = 1; i < observaciones.length()-1; i++)
		if (caracteres[i] == ' ' || caracteres[i] == '.' || caracteres[i] == ','){
		  caracteres[i + 1] = Character.toLowerCase(caracteres[i + 1]);
		 }
		else{
		caracteres[i] = Character.toLowerCase(caracteres[i]);
			}
		  return new String(caracteres);
	}
	def formatoProcedencia(params){
		def oCatalogoParticipante = new CatalogoParticipante(params)
		String procedencia = params.catPartProcedencia
		char[] caracteres =procedencia.toCharArray();
		caracteres[0] = Character.toUpperCase(caracteres[0]);
		for (int i = 1; i < procedencia.length()-1; i++)
		if (caracteres[i] == ' ' || caracteres[i] == '.' || caracteres[i] == ','){
		  caracteres[i + 1] = Character.toLowerCase(caracteres[i + 1]);
		 }
		else{
		caracteres[i] = Character.toLowerCase(caracteres[i]);
			}
		  return new String(caracteres);
	}
	def formatoCorreoElectronico(params){
		def oCatalogoParticipante = new CatalogoParticipante(params)
		String correo = params.catPartCorreoElectronico
		char[] caracteres =correo.toCharArray();		
		for (int i = 0; i < correo.length()-1; i++)
		if (caracteres[i] == ' ' || caracteres[i] == '.' || caracteres[i] == ','){
		  caracteres[i + 1] = Character.toLowerCase(caracteres[i + 1]);
		 }
		else{
		caracteres[i] = Character.toLowerCase(caracteres[i]);
			}
		  return new String(caracteres);	
	}
	def formatoCodigoUsuario(params){
		def oCatalogoParticipante = new CatalogoParticipante(params)
		String codigoUsuario = params.catPartCodigoUsuario
		char[] caracteres =codigoUsuario.toCharArray();
		for (int i = 0; i < codigoUsuario.length()-1; i++)
		if (caracteres[i] == ' ' || caracteres[i] == '.' || caracteres[i] == ','){
		  caracteres[i + 1] = Character.toLowerCase(caracteres[i + 1]);
		 }
		else{
		caracteres[i] = Character.toLowerCase(caracteres[i]);
			}
		  return new String(caracteres);
		
	}
	
}
